#!/user/bin/env python3
# -*- coding: utf-8 -*-

import os
import uuid

import cv2
import requests


# 绘图请求包装


def requestDrawPicture(x, img, color=None, label=None, ch_text=None, line_thickness=None):

    c1, c2 = (int(x[0]), int(x[1])), (int(x[2]), int(x[3]))
    # list里的ndarray对象转file
    url = 'http://127.0.0.1:5002/requestDrawing'
    path = 'F:/convertImage/lxy.jpg'

    if not os.path.exists(path):
        with open(path, mode='w', encoding='utf-8'):
            cv2.imwrite(path, img)
    else:
        # 删除文件
        os.remove(path)
        with open(path, mode='w', encoding='utf-8'):
            cv2.imwrite(path, img)

    data = {'c1': c1, 'c2': c2, 'color': color, 'label': label, 'ch_text': ch_text, 'line_thickness': line_thickness}
    string=requests.post(url, data=data)
    im0 = cv2.imread(path)
    return im0


def tensor_to_np(tensor):
    img = tensor.mul(255).byte()
    img = img.cpu().numpy().squeeze(0).transpose((1, 2, 0))
    return img
